package com.woniu.cms.contract.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.cms.contract.core.entity.UndertakeContract;
import com.woniu.cms.contract.core.entity.UndertakeContractManagement;
import com.woniu.cms.contract.core.mapper.UndertakeContractManagementMapper;
import com.woniu.cms.contract.core.service.IUndertakeContractManagementService;
import com.woniu.cms.contract.core.service.IUndertakeContractService;
import com.woniu.common.api.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maple leaf
 * @since 2022-09-17
 */
@Service
public class UndertakeContractManagementServiceImpl extends ServiceImpl<UndertakeContractManagementMapper, UndertakeContractManagement> implements IUndertakeContractManagementService {


    @Autowired
    private IUndertakeContractService undertakeContractService;

    /*
    * 分页与条件查询
    * */
    @Override
    public Result queryUnderMen(String undertakeContractCode, Integer currentPage, Integer pageSize) {
        Page<UndertakeContractManagement> page = new Page<>(currentPage, pageSize);

        QueryWrapper<UndertakeContractManagement> wrapper = new QueryWrapper<>();

        if (undertakeContractCode != null && undertakeContractCode != ""){
            wrapper.like("undertake_contract_code",undertakeContractCode);
        }

        Page<UndertakeContractManagement> undertakeContractManagementPage = this.page(page, wrapper);
        List<UndertakeContractManagement> records = undertakeContractManagementPage.getRecords();
        long total = undertakeContractManagementPage.getTotal();

        HashMap<String, Object> map = new HashMap<>();

        map.put("records",records);
        map.put("total",total);

        return Result.common(map);
    }

    /*
    * 添加数据
    * */
    @Override
    public Result addUnderMen(UndertakeContractManagement undertakeContractManagement) {

        boolean b = this.save(undertakeContractManagement);

        return b == true ? Result.ok() : Result.error();
    }

    /*
    * 根据编号查询
    * */
    @Override
    public Result queryByUnderMen(String undertakeContractCode) {

        QueryWrapper<UndertakeContractManagement> wrapper = new QueryWrapper<>();

        wrapper.eq("undertake_contract_code",undertakeContractCode);

        UndertakeContractManagement contractManagement = this.getOne(wrapper);

        return Result.common(contractManagement);
    }

    /*
    * 修改
    * */
    @Override
    public Result updaUnderMen(UndertakeContractManagement undertakeContractManagement) {

        QueryWrapper<UndertakeContract> wrapper = new QueryWrapper<>();

        wrapper.eq("Undertake_contract_code",undertakeContractManagement.getUndertakeContractCode());

        UndertakeContract contract = undertakeContractService.getOne(wrapper);
        if (contract.getContractType() == 1){
            contract.setContractState(1);
            boolean b = undertakeContractService.update(contract, wrapper);

        }
        QueryWrapper<UndertakeContractManagement> wrapper1 = new QueryWrapper<>();
        boolean b = this.update(undertakeContractManagement, wrapper1);


        return b == true ? Result.ok() : Result.error();
    }

    /*
    * 删除
    * */
    @Override
    public Result delUnderMen(String undertakeContractCode) {

        QueryWrapper<UndertakeContractManagement> wrapper = new QueryWrapper<>();

        wrapper.eq("undertake_contract_code",undertakeContractCode);

        boolean b = this.remove(wrapper);

        return b == true ? Result.ok() : Result.error();
    }
}
